 
# 导入OpenCV
import cv2

# 方块管理类
class SquareManager:
    def __init__(self):

 
        # 方块list
        self.square_count = 0
        self.rect_left_x_list = []
        self.rect_left_y_list = []
        self.alpha_list = []

        self.pic_list = []
        self.is_pic = []
        # 中指与矩形左上角点的距离
        self.L1 = 0
        self.L2 = 0
 
        # 激活移动模式
        self.drag_active = False
 
        # 激活的方块ID
        self.active_index = -1

    # 创建一个方块，但是没有显示
    def create(self, rect_left_x, rect_left_y, rect_width, rect_height, alpha=0.4, pic_address: str = None):
        # 方框长度

        self.rect_width_list.append(rect_width)
        self.rect_height_list.append(rect_height)

        self.rect_left_x_list.append(rect_left_x)
        self.rect_left_y_list.append(rect_left_y)
        self.alpha_list.append(alpha)
        self.square_count += 1

        if pic_address is not None:
            # 加载PNG图片
            pic = cv2.imread(pic_address, cv2.IMREAD_UNCHANGED)  # 使用cv2.IMREAD_UNCHANGED来保留透明通道  
            pic = cv2.resize(pic, (rect_width, rect_height))  # 500x200
            self.pic_list.append(pic)
            self.is_pic.append(1)

            # 如果PNG图片有透明通道，直接覆盖  
            if pic.ndim == 3 and pic.shape[2] == 4:  
                
        else:
            self.is_pic.append(0)


    # 更新位置
    def display(self, class_obj):
        for i in range(0, self.square_count):
            x = self.rect_left_x_list[i]
            y = self.rect_left_y_list[i]
            alpha = self.alpha_list[i]
            width = self.rect_width_list[i]
            height = self.rect_height_list[i]
 
            overlay = class_obj.image.copy()
 
            if (i == self.active_index):
                cv2.rectangle(overlay, (x, y), (x + width, y + height), (255, 0, 255), -1)
            else:
                cv2.rectangle(overlay, (x, y), (x + width, y + height), (255, 0, 0), -1)
 
            # Following line overlays transparent rectangle over the self.image
            class_obj.image = cv2.addWeighted(overlay, alpha, class_obj.image, 1 - alpha, 0)
 
    # 判断落在哪个方块上，返回方块的ID
    def checkOverlay(self, check_x, check_y):
        for i in range(0, self.square_count):
            x = self.rect_left_x_list[i]
            y = self.rect_left_y_list[i]
 
            width = self.rect_width_list[i]
            height = self.rect_height_list[i]

            if (x < check_x < (x + width)) and (y < check_y < (y + height)):
                # 保存被激活的方块ID
                self.active_index = i
 
                return i
 
        return -1
 
    # 计算与指尖的距离
    def setLen(self, check_x, check_y):
        # 计算距离
        self.L1 = check_x - self.rect_left_x_list[self.active_index]
        self.L2 = check_y - self.rect_left_y_list[self.active_index]
 
    # 更新方块    
    def updateSquare(self, new_x, new_y):
        # print(self.rect_left_x_list[self.active_index])
        self.rect_left_x_list[self.active_index] = new_x - self.L1
        self.rect_left_y_list[self.active_index] = new_y - self.L2
 
 